const express = require('express')
const multer = require('multer');
const banner = express.Router()
const fs = require("fs");
const path=require('path')
const img = multer({dest:__dirname +'/img'})
banner.use(img.any()) 
const {getBanneres,getBanner,postAddBanner,postDleteBanner,getBannerID,postUpdateBanner} = require('../../service/banner/bannerService')

//查询轮播图
banner.get('/es',getBanneres)
//查询轮播图
banner.get('/',getBanner)
//id查询轮播图
banner.get('/id',getBannerID)
//修改轮播图
banner.post('/update',postUpdateBanner)
//添加轮播图
banner.post('/add',postAddBanner)
//删除轮播图
banner.post('/delete',postDleteBanner)

//上传轮播图
banner.post('/img',async(req,res,next) =>{
  const files = req.files
  let oldfile = req.files[0].path
  let newfile = req.files[0].path +path.parse(req.files[0].originalname).ext
  fs.renameSync(oldfile,newfile)
  res.send({
    err:0,
    msg:'上传文件成功',
    // url:"http://127.0.0.1:3000/img/"+req.files[0].filename+path.parse(req.files[0].originalname).ext
    url:"http://47.119.123.95:3000/img/"+req.files[0].filename+path.parse(req.files[0].originalname).ext
  })
})
module.exports = banner